package com.uml.umlwork.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uml.umlwork.dto.news.NewsDTO;
import com.uml.umlwork.entity.News;
import com.uml.umlwork.service.NewsService;
import com.uml.umlwork.utils.PageResult;
import com.uml.umlwork.utils.SystemMsgJsonResponse;
import com.uml.umlwork.vo.news.NewsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Youens
 * @date 2020/12/21 2021-01-03 13:52
 */
@Api(tags = "新闻管理相关模块 - api")
@RequestMapping("/api/v1/umlwork/news")
@RestController
public class NewsController {

    private final NewsService newsService;

    public NewsController(NewsService newsService) {
        this.newsService = newsService;
    }

    @GetMapping("/getAllNews")
    @ApiOperation(value = "[管理员] - 查询新闻", httpMethod = "GET")
    public SystemMsgJsonResponse getAllNews(
            @RequestParam(value = "page", defaultValue = "1") Long current,
            @RequestParam(value = "size", defaultValue = "10") Long size,
            @RequestParam(value = "publish", defaultValue = "-1") Integer publish) {
        Page<NewsVO> result = newsService.getAllNews(current, size, publish == -1 ? null : publish);
        return SystemMsgJsonResponse.success(result);
    }

    @GetMapping("/releaseNews")
    @ApiOperation(value = "[管理员] - 修改新闻状态", httpMethod = "GET")
    public SystemMsgJsonResponse cancelReleaseNewsById(@RequestParam(value = "newsId") Integer id,
                                                       @RequestParam(value = "open") Integer open) {
        newsService.releaseNewsByNewsId(id, open.equals(1));
        return SystemMsgJsonResponse.success();
    }

    @PostMapping("/saveNews")
    @ApiOperation(value = "[管理员]-修改新闻", httpMethod = "POST")
    public SystemMsgJsonResponse saveNews(@RequestBody @Validated NewsDTO dto) {
        newsService.saveNews(dto);
        return SystemMsgJsonResponse.success();
    }
}
